import React from "react";
import {Select} from "antd";
import BaseComponent from "../../../../redare/components/BaseComponent";
import PlatformPresenter from "../../.gen/presenter/component/core/PlatformPresenter";

export default class PlatformSelect extends BaseComponent {

    static defaultProps = {
        allowClear: true,
        onValueChange: undefined,
        style: undefined
    };

    constructor(props) {
        super(props);
        this.state = {data: [], value: this.props.value};
        this.presenter = new PlatformPresenter(this);
    }

    componentDidMount() {
        this.presenter.findPlatformList({}, {returnAll: true});
    }

    UNSAFE_componentWillReceiveProps({value}, nextContext) {
        if (this.props.value !== value && this.state.value !== value) {
            this._onValueChange(value);
        }
    }

    _onFindPlatformListSuccess(result) {
        this.setState({
            data: result.data
        })
    }

    _onValueChange(value) {
        this.setState({value});
        if (this.props.onValueChange) {
            this.props.onValueChange(value);
        }
    }

    renderOption() {
        let options = [];
        this.state.data.forEach((v) => {
            options.push(
                <Select.Option key={v.id} value={v.platform}>{v.name}（{v.platform}）</Select.Option>
            )
        });
        return options;
    }

    render() {
        return (
            <Select allowClear={this.props.allowClear} value={this.state.value}
                    onChange={this._onValueChange.bind(this)}
                    style={this.props.style}>
                {
                    this.renderOption()
                }
            </Select>
        )
    }
}
